
//$(document).ready(function(){
$(window).load(function(){
    
    if (window.autocnames) {
        var autoc = new window.PolarRoseAutoComplete('saveinput',5,window.autocnames);
    };
    
    var objects = window.annotatedobjects || [];
    
    if ($("#image img").length) {
        var jcrop_api = $.Jcrop("#image img",{
            onChange: showThumb,
            onSelect: showThumb,
            boxHeight: 400
        });
    };
    
    function showThumb(coords) {
        if (coords && coords.w && coords.h) {
            $("#thumbs #save").show();
            var s = 75.0 / coords.h;
            if ((s*coords.w)>300) {
                s = 300 / coords.w;
            };
            $("#thumbs .thumb:last").css({
                width: Math.round(s*coords.w)+"px",
                height: Math.round(s*coords.h)+"px"
            }).show();
            $("#thumbs .thumb:last img").css({
                width: Math.round(s*$("#image img").width())+"px",
                height: Math.round(s*$("#image img").height())+"px",
                marginLeft: "-"+Math.round(s*coords.x)+"px",
                marginTop: "-"+Math.round(s*coords.y)+"px"
            });
            $("#thumbs #save input").focus();
        } else {
            $("#thumbs #save").hide();
            $("#thumbs .thumb:last").hide();
            if ($("#thumbs #save input").val() !== "enter name/id...") {
                $("#thumbs #save input").val("");
            };
        };
    };
    
    $("#thumbs #save input").focus(function(event){
        if ($(this).val() === "enter name/id...") {
            $(this).val("").css({"font-style":"normal"});
        };
        // TODO open autocomplete
    });
    
    $("#thumbs #save .button").click(function(event){
        var name = encodeURIComponent($.trim($("#thumbs #save input").val()));
        if (name) {
            var thumbclone = $("#thumbs .thumb:last").clone();
            $("#thumbs .thumb:last").after("<div class='name'>"+name+" <a class='delete' href='#'>X</a></div>");
            thumbclone.insertAfter($("#thumbs .name:last"));
            var coords = jcrop_api.tellSelect();
            coords["id"] = name;
            objects.push(coords);
            showThumb(null);
            jcrop_api.release();
        } else {
            $("#thumbs #save input").focus();
        };
    });
    
    $(document).keypress(function(e){
        if (e.which === 13 && $("#thumbs #save input").is(":visible")) {
            $("#thumbs #save .button").click();
        };
    });
    
    $("#thumbs .name .delete").live("click",function(e){
        var index = ($(this).parent().prevAll().size()-1)/2;
        objects.splice(index,1);
        $(this).parent().prev().remove();
        $(this).parent().remove();
    });
    
    // * * * * *
    
    $("#tags .taggroup a:first-child").click(function(event){
        if ($(this).hasClass("selected")) {
            $(this).siblings().andSelf().removeClass("selected");
        } else {
            $(this).addClass("selected");
        }
    });
    
    $("#tags .taggroup a:not(:first-child)").click(function(event){
        if ($(this).hasClass("selected")) {
            $(this).removeClass("selected");
        } else {
            $(this).siblings(":first").andSelf().addClass("selected");
        }
    });
    
    // * * * * *
    
    $("#next .button.previous").click(function(event){
        if (parseInt($("#next input[name=image]").attr("value"),10) > 1) {
            $("#next input[name=what]").attr("value","previous");
            $("#next form").submit();
        }
    });
    
    $("#next .button.index").click(function(event){
        $("#next input[name=what]").attr("value","index");
        $("#next form").submit();
    });
    
    $("#next .button.next").click(function(event){
        $("#next input[name=what]").attr("value","next");
        $("#next form").submit();
    });
    
    $("#next form").submit(function(event){
        // get tags
        var tags = [];
        $("#tags a.selected").each(function(i){
            tags.push($.trim($(this).text()));
        });
        $("#next input[name=tags]").attr("value",tags.toString());
        
        // get objects
        var o = "";
        for (var i=0, leni=objects.length; i<leni; i++) {
            if (i!==0) o += ",";
            o += "id="+objects[i].id;
            o += "&left="+objects[i].x;
            o += "&right="+objects[i].x2;
            o += "&top="+objects[i].y;
            o += "&bottom="+objects[i].y2;
        }
        $("#next input[name=objects]").attr("value",o);
    });
    
    $(document).keypress(function(e){
        if (e.keyCode === 37 && !$("#thumbs #save input").is(":visible")) {
            $("#next .button.previous").click();
        } else if (e.keyCode === 39 && !$("#thumbs #save input").is(":visible")) {
            $("#next .button.next").click();
        } else if (e.keyCode === 38 && !$("#thumbs #save input").is(":visible")) {
            $("#next .button.index").click();
        };
    });
    
    // * * * * *
    
    $("#init input").focus(function(event){
        if ($(this).val() === "enter local folder path...") {
            $(this).val("").css({"font-style":"normal"});
        };
    });
    
    $("#init .button").click(function(event){
        var albumpath = $("#init input[name=albumpath]").val();
        if (albumpath && albumpath!=="enter local folder path...") {
            $("#init form").submit();
        };
    });
    
    /*$("#init form").submit(function(event){
        //
    });*/
    
});
